extends CharacterBody2D

const FileEx = preload("res://Script/Extension/FileEx.gd")
const StringEx = preload("res://Script/Extension/StringEx.gd")
const ArrayEx = preload("res://Script/Extension/ArrayEx.gd")

var storePath = "user://storeData.txt"
var speed = 400
var screen_size

func _ready():
	screen_size = get_viewport_rect().size
	# print("One,Two,Three,Four".split(","))
	# print(",, ,".split(","))
	# print(",, ,".split(",", false))
	var pos = FileEx.readStr(storePath).split_floats(",", false)
	print("Player Init Position: ", self.position)
	print("Player Store Position: ", pos)
	if !ArrayEx.isEmpty(pos):
		self.position = Vector2(pos[0], pos[1])

func _process(delta):
	var v = Vector2.ZERO
	if Input.is_action_pressed("ui_right"):
		v.x += 1
	if Input.is_action_pressed("ui_left"):
		v.x -= 1
	if Input.is_action_pressed("ui_down"):
		v.y += 1
	if Input.is_action_pressed("ui_up"):
		v.y -= 1
	if v.length() <= 0:
		return
	v = v.normalized() * speed
	var info: KinematicCollision2D = move_and_collide(v * delta, false)
	if info != null:
		print(info.get_collider())

func _notification(what):
	if what == NOTIFICATION_WM_CLOSE_REQUEST:
		var pos = Vector2i(self.position)
		FileEx.writeStr(storePath, StringEx.format("{},{}", [pos.x, pos.y]))
